package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.system.service.IApplyTableService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ApplySet;
import com.ruoyi.system.service.IApplySetService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 表格记录Controller
 * 
 * @author ruoyi
 * @date 2021-11-14
 */
@Controller
@RequestMapping("/set/set")
public class ApplySetController extends BaseController
{
    private String prefix = "set/set";

    @Autowired
    private IApplySetService applySetService;

    @Autowired
    private IApplyTableService applyTableService;

    @RequiresPermissions("set:set:view")
    @GetMapping()
    public String set()
    {
        return prefix + "/set";
    }

    /**
     * 查询表格记录列表
     */
    @RequiresPermissions("set:set:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ApplySet applySet)
    {
        startPage();
        List<ApplySet> list = applySetService.selectApplySetList(applySet);
        return getDataTable(list);
    }

    /**
     * 导出表格记录列表
     */
    @RequiresPermissions("set:set:export")
    @Log(title = "表格记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ApplySet applySet)
    {
        List<ApplySet> list = applySetService.selectApplySetList(applySet);
        ExcelUtil<ApplySet> util = new ExcelUtil<ApplySet>(ApplySet.class);
        return util.exportExcel(list, "表格记录数据");
    }

    /**
     * 新增表格记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存表格记录
     */
    @RequiresPermissions("set:set:add")
    @Log(title = "表格记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ApplySet applySet)
    {
        if(applySet.getIsOuter().equals("1")){
            applySet.setPalceArea("(室内)"+ applySet.getPalceArea() + "平方米");
        }else {
            applySet.setPalceArea("(室外)"+ applySet.getPalceArea() + "平方米");
        }
        return toAjax(applySetService.insertApplySet(applySet));
    }

    /**
     * 修改表格记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ApplySet applySet = applySetService.selectApplySetById(id);
        mmap.put("applySet", applySet);
        return prefix + "/edit";
    }

    /**
     * 修改保存表格记录
     */
    @RequiresPermissions("set:set:edit")
    @Log(title = "表格记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ApplySet applySet)
    {
        return toAjax(applySetService.updateApplySet(applySet));
    }

    /**
     * 删除表格记录
     */
    @RequiresPermissions("set:set:remove")
    @Log(title = "表格记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(applySetService.deleteApplySetByIds(ids));
    }

}
